package com.yc.bdw.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.yc.bdw.R;
import com.yc.bdw.model.SituationItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PaymentListAdapter extends BaseAdapter {

	private static HashMap<Integer,Boolean> isSelected;

	private List<SituationItem> list = new ArrayList<SituationItem>();
	private LayoutInflater inflater = null;
	private Context context = null;

	private int which = -1;

	public PaymentListAdapter(){}

	public PaymentListAdapter(Context context) {
		inflater = LayoutInflater.from(context); 
		this.context = context;
	}

	public void init() {
		isSelected = new HashMap<Integer, Boolean>();
		for ( int i = 0; i < list.size(); i++ ) {
			isSelected.put(i,false);
		}
	}
	
	@Override
	public int getCount() {
		if ( list != null ) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static class PaymentItem {
		ImageView img;
		TextView name;
		CheckBox select;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		PaymentItem paymentItem;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_payment, null);
			paymentItem = new PaymentItem();
			paymentItem.img = (ImageView) convertView.findViewById(R.id.item_payment_img);
			paymentItem.name = (TextView) convertView.findViewById(R.id.item_payment_name);
			paymentItem.select = (CheckBox) convertView.findViewById(R.id.item_payment_select);

			convertView.setTag(paymentItem);
		} else {
			paymentItem = (PaymentItem) convertView.getTag();
		}

		paymentItem.name.setText(list.get(position).getName());
//		String path = list.get(position).getImagePath();
//		aKeyOfWineItem.img.getLayoutParams().width = ListUtils.dip2px(context, 100);
//		aKeyOfWineItem.img.getLayoutParams().height = ListUtils.dip2px(context, 120);
//		KJBitmapUtils.getObj().display(productItem.img, path);

		paymentItem.select.setChecked(isSelected.get(position));
		paymentItem.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton compoundButton, boolean flag) {
				if ( flag ) {
					which = position;
					for ( int i = 0; i < isSelected.size(); i++ ) {
						if ( position == i ) {
							isSelected.put(i,true);
						} else {
							isSelected.put(i,false);
						}
					}
					notifyDataSetChanged();
				}
			}
		});

		return convertView;
	}
	
	public List<SituationItem> getList() {
		return list;
	}

	public void setList(List<SituationItem> list) {
		this.list = list;
	}

	public int getWhich() {
		return which;
	}

	public void setWhich(int which) {
		this.which = which;
	}
}